package screen;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;

import common.Constant;

import resources.Input;

public class GameScreen extends Screen  {
	
	public static final int GAME_RUNNING = 1;
	public static final int GAME_PAUSED = 2;
	public static final int GAME_LEVEL_END = 3;
	public static final int GAME_OVER = 4;
	private int state;
	
	public static int GENERATE_OBJ_ID;
	
	protected Game game;
	private OrthographicCamera waveCamera;
	private LoadDataThread thread;
	private Vector3 touchPoint;
	private Stage stage;
	private int old_level = 0;
	public int level;
	
	public Texture tetrisSlash;
	public TextureRegion tetrisSlashRegion;
	public Texture bgLevel1;
	public TextureRegion bgLevelRegion;
	
	private boolean isLoadData = false;
	private boolean isLoad = false;
	private boolean loaded = false;
	
	public GameScreen (Game game) {
		super(game);
//		System.out.println("1");
		game.setNumSreen(Constant.GameScreen);
//		System.out.println("2");
		waveCamera = new OrthographicCamera(Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
		waveCamera.position.set(Constant.SCREEN_WIDTH / 2, -100, 0);
		
		state = GAME_RUNNING;
		touchPoint = new Vector3();
		thread = new LoadDataThread();
	}
	
	public void initilize(int level){
		this.level = level;
		this.old_level = level;
		loadingSplash();
	}	
	
	private void loadingSplash(){
		tetrisSlash = new Texture("data/bgGames1.png");
		tetrisSlashRegion = new TextureRegion(tetrisSlash, 0, 0, 
				1024, 512);
		bgLevel1 = new Texture("data/bgUI.png");
		bgLevelRegion = new TextureRegion(bgLevel1 ,0 , 0, 512,
				1024);
	}
	
	private void unloadSplash(){		
		if(tetrisSlash != null) tetrisSlash.dispose();
	}
	
	public void tick(Input input){// tuong tu ham update
		if(!isLoadData){
			thread.run();
			isLoadData = true;
		}
		if(thread != null && thread.isAlive()){
			thread = null;
			System.gc();
		}
		if(isLoad){
			if(!loaded){
				
			}
		}
	}
	
		
	@Override
	public void present () {
//		System.out.println("Game screen rendering");
		if(loaded){
			camera.update();
			spriteBatch.setProjectionMatrix(camera.combined);
			spriteBatch.begin();
			System.out.println("present");
			spriteBatch.draw(bgLevelRegion, 0, 0);
			stage.render(spriteBatch);
			presentToolbar();
			stage.renderer.setProjectionMatrix(camera.combined);
			spriteBatch.end();
		} else {
			camera.update();
			spriteBatch.setProjectionMatrix(camera.combined);
			spriteBatch.begin();
			spriteBatch.draw(tetrisSlashRegion, 0, 0);
			spriteBatch.draw(bgLevelRegion, 0, -370);
			spriteBatch.end();
		}
	}
	/*
	 * Draw tool bar cho man hinh game
	 */
	private void presentToolbar(){
		
	}
	
	@Override
	public void pause () {
		
	}

	@Override
	public void resume () {
		
	}

	@Override
	public void dispose () {
	}
	
	public class LoadDataThread extends Thread{
		@Override
		public void run() {
			
		}
	}	
}

